/**
 * @param {string} s
 * @return {number[]}
 */
var partitionLabels = function(s) {
  let ans = [];
  const map = new Map();

  // 保存每个字符出现的最远位置
  for (let i = 0; i < s.length; i++) {
    map.set(s[i], i);
  }

  let right = 0, left = 0;
  for (let i = 0; i < s.length; i++) {
    right = Math.max(right, map.get(s[i]));
    if (right === i) {
      ans.push(right - left + 1);
      left = right + 1;
    }
  }
  return ans;
    
};

const s = "ababcbacadefegdehijhklij";
console.log(partitionLabels(s));